package week_four;

import java.util.PriorityQueue;

public class FindMedianFromDataStream_295 {
    private int count;
    private PriorityQueue<Integer> maxheap;
    private PriorityQueue<Integer> minheap;

    /**
     * initialize your data structure here.
     */
    public FindMedianFromDataStream_295() {
        count = 0;
        maxheap = new PriorityQueue<>((x, y) -> y - x);
        minheap = new PriorityQueue<>();
    }

    public void addNum(int num) {
        count += 1;
        maxheap.offer(num);
        minheap.offer(maxheap.poll());
        if (count % 2 != 0) {
            maxheap.offer(minheap.poll());
        }
    }

    public double findMedian() {
        if (count % 2 != 0) {
            return maxheap.peek();
        } else
            return (double) (maxheap.peek() + minheap.peek()) / 2;
    }

    public static void main(String[] args) {
        FindMedianFromDataStream_295 obj = new FindMedianFromDataStream_295();


        obj.addNum(1);
        double param_2 = obj.findMedian();
        System.out.println(param_2);
        obj.addNum(2);
        param_2 = obj.findMedian();
        System.out.println(param_2);
        obj.addNum(3);
        param_2 = obj.findMedian();
        System.out.println(param_2);

    }
}

/**
 * Your MedianFinder object will be instantiated and called as such:
 * FindMedianFromDataStream_295 obj = new FindMedianFromDataStream_295();
 * obj.addNum(num);
 * double param_2 = obj.findMedian();
 */
